<?php

/**
 * 完美分页类
 */
class PageService {

    private $total; //数据表中总记录数
    private $limit; //SQL语句使用limit从句，限制获取的记录数
    private $listRows; //每页显示行数
    private $page; //当前页
    private $pagenum; //总页数
    private $congfig = array(
        "head" => "条排期",
        "first" => "首页",
        "last" => "末页",
        "previous" => "上一页",
        "next" => "下一页"
    ); //在分页信息中显示内容
    private $listNum = 10; //默认分页列表显示的个数

//    private $previous;//前一页
//    private $next;//下一页
//    private $date;
    /**
     * 构造方法，设置分页类属性
     * @param type $total
     * @param type $listRow
     * @param int $page
     * @param type $ord
     */

    public function __construct($total, $listRow = 25, $page, $ord = TRUE) {
        $this->total = $total;
        $this->listRows = $listRows;
        $this->pageNum = ceil(($this->total)/($this->listRows));
//        if($page!=null){
//            $this->page=$page;
//        }else{
        //以下设置查询页码
        if ($ord) {
            $page = 1;
        } else {
            $page = $this->pagenum;
        }
        //  }
        if ($this->total > 0) {
            if (preg_match('/\D+/', $page)) {
                $this->page = 1;
            } else {
                $this->page = $page;
            }
        } else {
            $this->page = 0;
        }
        $this->limit = "LIMIT" . $this->setLimit(); //设置limit
//        $this->setTotal();
//        $this->setLimit(15);
//        $this->$pagenum = ceil($value);
    }

    /**
     * 用于设置显示分页的信息
     * @param type $param 成员属性config的下标
     * @param type $value设置config下标对应的元素值
     * @return \PageService返回对象本身
     */
    function set($param, $value) {
        if (array_key_exists($param, $this->congfig)) {
            $this->congfig[$param] = $value;
        } return $this;
    }

    /**
     * 获取limit和page的属性值，不是直接去调用，而是通过该方法
     * @param type $args
     * @return null
     */
    function __get($args) {
        if ($args == "limit" || $args == "page") {
            return $this->$args;
        } else {
            return NULL;
        }
    }

    /**
     * 获取当前页结束的记录数
     * @return type
     */
    private function end() {
        return min($this->page * $this->listRows, $this->total);
    }

    /**
     * 获取当前页面开始的记录数
     * @return int
     */
    private function start() {
        if ($this->total == 0) {
            return 0;
        } else {
            return ($this->page - 1) * ($this->listRows) + 1;
        }
    }

    /**
     * 获取本页显示的记录条数
     */
    private function disnum() {
        if ($this->total > 0) {
            return $this->end() - $this->start() + 1;
        } else {
            return 0;
        }
    }

    /**
     * 获取上一页和首页的记录数
     */
    private function firstprev() {
        if ($this->page > 1) {
            $str = "&nbsp;<a href='{Yii::app()->request->baseUrl}/index.php/schedule/getOnePageSchedule?'>";
            $str .="";
            return $str;
        }
    }

    /**
     * 获取页数列表信息
     */
    private function pageList() {
        
    }

    /**
     * 获取下一页或者尾页操作信息
     */
    private function nextlast() {
        if ($this->page != $this->pagenum) {
            $str = "";
            $str .="";
            return $str;
        }
    }

    /**
     * 显示和处理表单跳转页面
     */
    private function goPage() {
        
    }

    function fpage() {
        $arr = func_get_args(); //获得一个函数的所有参数
        $html[0] = "&nbsp;共<b>{$this->total}</b>{$this->congfig["head"]}&nbsp;";
        $html[1] = "&nbsp;本页<b>{$this->disnum()}</b>{$this->congfig["head"]}&nbsp;";
        $html[2] = "&nbsp;本页从<b>{$this->start()}-{$this->end()}</b>条&nbsp;";
        $html[3] = "&nbsp;<b>{$this->page}/{$this->pagenum}</b>条&nbsp;";
        $html[4] = $this->firstprev();
        $html[5] = $this->pageList();
        $html[6] = $this->nextlast();
        $html[7] = $this->goPage();
        $fpage = '<div style="font:12px \'\5B8B\4F53\',san-serif;">';
        if (count($arr) < 1) {
            $arr = array(0, 1, 2, 3, 4, 5, 6, 7);
        }
        for ($i = 0; $i < count($arr); $i++) {
            $fpage .=$html[$arr[$i]];
        }
        $fpage .='</div>';
        return $fpage;
    }


    private function setLimit() {
        if ($this->page > 0) {
            return ($this->page - 1) * $this->listRows . ",{$this->listRows}"; //拼接SQL代码limit
        }
        $this->limit = 0;
    }

    function getpagedata($page) {
        
    }

}

?>
